#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies (www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, All Rights Reserved.
// Assembly: Engine
// Code File Name: UsageWatcher.cs
// Code File Date: 10/8/2006
//
// Code File Info: Usage System
//=============================================================================
/*
 * UPDATES
 * -------
 *                  ShawnJ 5/5/2007 (0.9.0.x): Refactoring for OSS.
 */
#endregion Header

#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Tools;
using WaveTech.GShield.Model;

#endregion

namespace WaveTech.GShield.Engine
{
	/// <summary>
	/// ESRB Game Ratings Processor
	/// </summary>
	public class Ratings
	{
		#region Get Game Rating

		/// <summary>
		/// Retrieves a Game's ESRB Ratings from a GameProcess object
		/// </summary>
		/// <param name="gameProcess">Game Process Information</param>
		/// <returns>RatingTypes enumeration</returns>
		public RatingTypes GetGameRating(GameProcess gameProcess)
		{
			if (gameProcess == null)
				throw new ArgumentNullException("gameProcess");

			Game g = null;
			RatingTypes? rating = null;

			if (GamesDAO.GetGameByPath(gameProcess.FullPath) != null)
			{
				g = GamesDAO.GetGameByPath(gameProcess.FullPath);
				return (RatingTypes) g.RatingType;
			}

			if (g == null)
			{
				// Path should work, but if it was moved, get by internal stuff
				g = GamesDAO.GetGameByProductNameCompany(gameProcess.ProductName, gameProcess.Company);
				return (RatingTypes) g.RatingType;
			}

			if (g == null)
			{
				// Can't Find it locally
				Advisor advisor = new Advisor();
				rating = advisor.GetESRBRating(gameProcess.InternalName, gameProcess.Company,
				                               StringTools.GetFileName(gameProcess.Process.MainModule.FileName),
				                               StringTools.GetDirectoryName(gameProcess.Process.MainModule.FileName, true),
				                               gameProcess.ProductName);
			}

			if (rating == null)
				return RatingTypes.Unknown;
			else
				return rating.Value;
		}

		#endregion Get Game Rating
	}
}